/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.sdk;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;
import org.eclipse.core.runtime.FileLocator;
import org.zoundry.gaedt.core.util.ZGaeCloser;
import org.zoundry.gaedt.core.util.ZGaeFileUtil;
import org.zoundry.gaedt.core.util.ZGaeUtil;

/**
 * This singleton models the downloaded and unpacked GAE SDK.
 */
public class ZGaeSDK
{
    /** The singleton instance. */
    private static final ZGaeSDK sInstance = new ZGaeSDK();

    /**
     * Singleton getter.
     */
    public static ZGaeSDK getInstance()
    {
        return sInstance;
    }

    /** The version of the current SDK. */
    private String mVersion;

    /**
     * Private singleton c'tor.
     */
    private ZGaeSDK()
    {
        refresh();
    }

    /**
     * Gets the SDK location.
     */
    public String getLocation()
    {
        try
        {
            URL configDirUrl = FileLocator.resolve(new URL("platform:/config/"));
            String configDirPath = configDirUrl.toExternalForm().substring("file:/".length());
            File configDir = new File(configDirPath);
            File sdkDir = new File(configDir, ZGaeSDKActivator.PLUGIN_ID + "/sdk");
            if (!sdkDir.exists())
                ZGaeFileUtil.makeDirs(sdkDir);
            return sdkDir.getCanonicalPath();
        }
        catch (Throwable t)
        {
            ZGaeUtil.logError(t);
            return ZGaeSDKActivator.getDefault().getStateLocation().makeAbsolute().toString();
        }
    }

    /**
     * Gets the version of the downloaded SDK.
     */
    public String getVersion()
    {
        return mVersion;
    }

    /**
     * Refreshes the SDK's meta data (by reading the gaesdk.properties file).
     */
    public void refresh()
    {
        String version = null;

        File sdkPropsFile = new File(getLocation(), "gaesdk.properties");
        if (sdkPropsFile.isFile())
        {
            Properties sdkProps = new Properties();
            InputStream iStream = null;
            try
            {
                iStream = new FileInputStream(sdkPropsFile);
                sdkProps.load(iStream);
                version = sdkProps.getProperty("version");
            }
            catch (Throwable t)
            {
                ZGaeUtil.logError(t);
            }
            finally
            {
                ZGaeCloser.close(iStream);
            }
        }

        setVersion(version);
    }

    /**
     * @param aVersion the version to set
     */
    protected void setVersion(String aVersion)
    {
        mVersion = aVersion;
    }
}
